package com.atguigu.gmall.order.controller;

import com.atguigu.gmall.cart.model.CartInfo;
import com.atguigu.gmall.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-01-11 10:46
 */
@RestController
@RequestMapping("/api/cart")
public class CarApiController {


    @Autowired
    private CartService cartService;


    /**
     * 用户将商品加入到购物车
     *
     * @param skuId  商品SKUID
     * @param skuNum 商品加购的数量
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum) {
        //声明用户ID遍历：可能是登录用户ID也可能是临时用户ID
        String userId = "";
        userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addToCart(userId, skuId, skuNum);
        return Result.ok();
    }

    /**
     * 查询用户购物车列表
     * 版本1：分别查询未登录购物车列表，以及登录的购物车列表
     * 版本2：将两个购物车中商品合并
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result<List<CartInfo>> cartList(HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartInfoList = cartService.cartList(userId, userTempId);
        return Result.ok(cartInfoList);
    }


    /**
     * 变更购物车商品选中状态
     * @param skuId 商品SKUID
     * @param isChecked 状态
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("isChecked") Integer isChecked){
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isBlank(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.checkCart(userId, skuId, isChecked);
        return Result.ok();
    }

    /**
     * 删除购物车商品
     * @param request
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(HttpServletRequest request, @PathVariable("skuId") Long skuId){
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isBlank(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.deleteCart(userId, skuId);
        return Result.ok();
    }


    /**
     * 查询用户购物车中已勾选的商品列表
     * @param userId
     * @return
     */
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") Long userId){
        List<CartInfo> list = cartService.getCartCheckedList(userId);
        return list;
    }
}
